/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mh.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import mh.bol.ManageEmployeeBO;
import mh.entity.EmployeeEn;

/**
 *
 * @author VIETNH
 */
@ManagedBean
@RequestScoped
public class LoginRequest {

    /** Creates a new instance of LoginRequest */
    public LoginRequest() {
    }
    ManageEmployeeBO meBO = new ManageEmployeeBO();
    private EmployeeEn empEn;
    private String userName;
    private String password;

    public EmployeeEn getEmpEn() {
        return empEn;
    }

    public void setEmpEn(EmployeeEn empEn) {
        this.empEn = empEn;
    }

    public ManageEmployeeBO getMeBO() {
        return meBO;
    }

    public void setMeBO(ManageEmployeeBO meBO) {
        this.meBO = meBO;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String checkLogin() {
        boolean flag = meBO.checkLogin(userName, password);
        if (flag) {
            empEn = meBO.viewEmpByUserName(userName);
            if (empEn != null) {
                FacesContext facesContext = FacesContext.getCurrentInstance();
                ValueBinding binding = facesContext.getApplication().createValueBinding("#{loginSession}");
                LoginSession loginSession = (LoginSession) binding.getValue(facesContext);
                loginSession.setEmpEn(empEn);
                loginSession.setEid(empEn.getEid() + "");
                if (empEn.getEtype() == 3) {
                    return "index.xhtml";
                }
                return "AdListComplaint.xhtml";
            }
            System.out.println("empEn is null !");
            return "error.xhtml";
        } else {
            return "error.xhtml";
        }
    }
}
